package cn.detu.boot23.conteoller;


import cn.detu.boot23.utils.DBUtils;
import cn.detu.boot23.vo.Emp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.relation.RelationService;
import java.sql.*;
import java.util.ArrayList;

@RestController
public class EmpController {
    @RequestMapping("/insert")
    public String insert(Emp emp){
        try (Connection conn = DBUtils.getConn()){
            String sql = "insert into myemp values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成<a href='/'>回到首页</a>";
    }

    @RequestMapping("/select")
    public String select(){
        ArrayList<Emp> list = new ArrayList<>();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select * from myemp";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()){
                int id =rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                int sal = rs.getInt(4);
                list.add(new Emp(id,name,job,sal));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html ="<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>姓名</th><th>工作</th><th>工资</th><th>操作</th><tr>";
        for (Emp emp : list) {
            html+="<tr>";
            html+="";
            html+="";
            html+="";
            html+="";
            html+="";
            html+="";
        }
        return "查询完成<a href='/'>回到首页</a>";
    }
}
